package com.yxkj.syh.app.huarong.activities.creat.add_goods;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bean.LiveDataEvent;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.bean.GoodsResponse;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;

import java.math.BigDecimal;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/5/21 16:22
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class AddGoodsVM extends BaseViewModel<ActivityEvent> {

    private List<String> addedCodes;
    private Goods selectedGoods;

    public AddGoodsVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofPack = new ObservableField<String>("1");
    public ObservableField<String> ofWeight = new ObservableField<String>("1");
    public ObservableField<Boolean> ofRemoveEnable = new ObservableField<>(Boolean.FALSE);
    public ObservableField<Boolean> ofPartReturn = new ObservableField<>(Boolean.FALSE);

    public MutableLiveData<List<Goods>> mldGoods = new MutableLiveData<>();
    public MutableLiveData<LiveDataEvent> mldOperate = new MutableLiveData<>();

    public ClickEvent plus1Click = new ClickEvent() {
        @Override
        public void onClick(View view) {
            packClick(true);
        }
    };
    public ClickEvent plus2Click = new ClickEvent() {
        @Override
        public void onClick(View view) {
            weightClick(true);
        }
    };
    public ClickEvent reduce1Click = new ClickEvent() {
        @Override
        public void onClick(View view) {
            packClick(false);
        }
    };
    public ClickEvent reduce2Click = new ClickEvent() {
        @Override
        public void onClick(View view) {
            weightClick(false);
        }
    };
    public ClickEvent confirmClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (selectedGoods != null) {
                if (TextUtils.isEmpty(ofWeight.get())) {
                    Tooast.normalInfo("请输入重量");
                } else {
                    BigDecimal weight = new BigDecimal(ofWeight.get());
                    if (weight.compareTo(BigDecimal.ZERO) < 1) {
                        Tooast.normalInfo("重量不能为0");
                    } else {
                        if (!TextUtils.isEmpty(selectedGoods.getNo()) && selectedGoods.getNo().equals("custom") && selectedGoods.getName().contains("自定义")) {//自定义货品
                            Tooast.normalInfo("请输入自定义货品名称并确定");
                        } else {
                            for (Goods goods : mldGoods.getValue()) {
                                if (goods.getCode().equals(selectedGoods.getCode())) {
                                    selectedGoods.setWeight(weight);
                                    selectedGoods.setQuantity(weight);
                                    selectedGoods.setProductId(Long.decode(goods.getCode()));
                                    selectedGoods.setProductNo(goods.getVal());
                                    selectedGoods.setIsReturn(ofPartReturn.get() ? 1 : 0);
                                    if (goods.isAdded()) {
                                        mldOperate.setValue(new LiveDataEvent(2, selectedGoods));//编辑
                                    } else {
                                        mldOperate.setValue(new LiveDataEvent(0, selectedGoods));//添加
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            } else {
                Tooast.normalInfo("请选择货品");
            }
        }
    };
    public ClickEvent removeClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (selectedGoods != null) {
                for (Goods goods : mldGoods.getValue()) {
                    if (goods.getCode().equals(selectedGoods.getCode()) && goods.isAdded()) {
                        selectedGoods.setProductId(Long.decode(goods.getCode()));
                        mldOperate.setValue(new LiveDataEvent(1, selectedGoods));
                        return;
                    }
                }
                Tooast.normalInfo("未添加货品不能删除");
            }
        }
    };

    public void setAddedCodes(List<String> addedCodes) {
        this.addedCodes = addedCodes;
    }

    private void packClick(boolean isPlus) {
        BigDecimal packBD;
        if (TextUtils.isEmpty(ofPack.get())) {
            packBD = BigDecimal.ZERO;
        } else {
            packBD = new BigDecimal(ofPack.get());
        }
        if (isPlus) {
            packBD = packBD.add(BigDecimal.ONE);
        } else {
            packBD = packBD.subtract(BigDecimal.ONE);
        }
        if (packBD.compareTo(BigDecimal.ZERO) < 0) {
            packBD = BigDecimal.ZERO;
        }
        ofPack.set(ComUtil.clearNoUseZero(packBD).toPlainString());
    }

    private void weightClick(boolean isPlus) {
        BigDecimal weightBD;
        if (TextUtils.isEmpty(ofWeight.get())) {
            weightBD = BigDecimal.ZERO;
        } else {
            weightBD = new BigDecimal(ofWeight.get());
        }
        if (isPlus) {
            weightBD = weightBD.add(BigDecimal.ONE);
        } else {
            weightBD = weightBD.subtract(BigDecimal.ONE);
        }
        if (weightBD.compareTo(BigDecimal.ZERO) < 0) {
            weightBD = BigDecimal.ZERO;
        }
        ofWeight.set(ComUtil.clearNoUseZero(weightBD).toPlainString());
    }

    public void goodsList() {
        showLoading("goodsList");
        OrderModel.getOrderModel().goodsList(mLifecycleProvider.bindToLifecycle(), new ApiObserver<GoodsResponse>() {
            @Override
            public void onSuccess(GoodsResponse goodsResponse) {
                hideLoading("goodsList");
                mldGoods.setValue(goodsResponse.getData());
                for (Goods goods : goodsResponse.getData()) {
                    goods.setAdded(false);
                    goods.setLableStatus(0);
                    goods.setProductId(Long.decode(goods.getCode()));
                    if (addedCodes != null) {
                        for (String c : addedCodes) {
                            if (c.contains(goods.getCode())) {
                                goods.setAdded(true);
                                if (selectedGoods != null &&
                                        (selectedGoods.getCode().equals(goods.getCode()) || String.valueOf(selectedGoods.getProductId()).equals(goods.getCode()))) {
                                    ofRemoveEnable.set(true);
                                    goods.setLableStatus(2);
                                } else {
                                    goods.setLableStatus(1);
                                }
                                break;
                            }
                        }
                    }
                }
                mldGoods.setValue(goodsResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
                hideLoading("goodsList");
            }
        });
    }

    public void setSelectedGoods(Goods selectedGoods) {
        this.selectedGoods = selectedGoods;
    }

    public Goods getSelectedGoods() {
        return selectedGoods;
    }
}
